<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="brandDialogFormRef" :rules="brandRules" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="商品分类" prop="cate_id">
							<el-select v-model="state.ruleForm.cate_id" filterable placeholder="请选择分类" class="w100">
								<el-option
								v-for="item in state.categoryData"
								:key="item.id"
								:label="item.category_name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="品牌名称" prop="brand_name">
							<el-input v-model="state.ruleForm.brand_name" placeholder="请输入品牌名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="品牌图片" prop="brand_image">
							<el-upload class="h100 personal-user-left-upload avatar-uploader" 
									:show-file-list="false" 
									:http-request="handleUpload"
									:limit="1" 
									auto-upload="false"
									name="file">
								<img v-if="state.ruleForm.brand_image" :src="state.ruleForm.brand_image" class="avatar" />
    							<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
							</el-upload>

						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sort" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.is_show" :active-value="1" :inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="品牌描述">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入品牌描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="goodsBrandDialog">
import { onMounted, reactive, ref, nextTick } from 'vue';
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import  categoryApi  from '/@/api/goods/category/index.ts';
import  brandApi  from '/@/api/goods/brand/index.ts';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const brandDialogFormRef = ref();
const initRuleForm = reactive({
	id:'',
	cate_id:'',
	brand_name:'',
	brand_image:'',
	sort: 0, 
	is_show: 1, 
	describe: '', 
})
const state = reactive({
	ruleForm: initRuleForm,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	categoryData: [], 
	uploadAction: ''
});

const brandRules =  reactive({
	brand_name: [{required: true, message: "请输入品牌名称", trigger: 'blur'}],
	cate_id: [{required: true, message: "请请选择分类", trigger: 'blur'}]
});

const getCategoryData = async () => {
	const res = await categoryApi.listCategory();
	state.categoryData = res.data;
};

const handleUpload = async (file: any) => {
	const fromData = new FormData();
	fromData.append('file', file.file);
	const res = await brandApi.uploadBrandLogo(fromData);
	state.ruleForm.brand_image = res.data.url
}


// 打开弹窗
const openDialog = (type: string, row: any) => {
	state.ruleForm = initRuleForm;
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改品牌';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增品牌';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
	getCategoryData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	if (state.dialog.type === 'add') {
		const res = await brandApi.addBrand(state.ruleForm);
		ElMessage.success(res.msg);
	}else{
		const res = await brandApi.editBrand(state.ruleForm);
		ElMessage.success(res.msg);
	}
	closeDialog();
	emit('refresh');
};

// 暴露变量
defineExpose({
	openDialog,
});

onMounted(() => {
	
})
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
<style>
.avatar-uploader .avatar {
  width: 102px;
  height: 36px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 102px;
  height: 36px;
  text-align: center;
}
</style>
